package com.ltx.blog_ltx.controller;


import com.alibaba.fastjson.JSONObject;
import com.ltx.blog_ltx.entity.pojo.Collect;
import com.ltx.blog_ltx.mapper.BlogMapper;
import com.ltx.blog_ltx.mapper.CollectMapper;
import com.ltx.blog_ltx.mapper.UserMapper;
import com.ltx.blog_ltx.until.jwtUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CollectController {

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/AttentionBlog")
    public int AttentionBlog(@RequestBody JSONObject param){
        int blogId = param.getInteger("blogId");
        System.out.println(blogId);
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(tokenName);
        int collectId = (int) ((Math.random() * 9 + 1) * 100000);
        Collect collect = new Collect(collectId, userId, blogId);
        collectMapper.attentionBlog(collect);
        blogMapper.AddCollect(blogId);
        return blogMapper.CollectNum(blogId);
    }

    @PostMapping("/Unfollow")
    public int Unfollow(@RequestBody JSONObject param){
        int blogId = param.getInteger("blogId");
        System.out.println(blogId);
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(tokenName);
        collectMapper.unfollow(blogId,userId);
        blogMapper.musCollect(blogId);
        return blogMapper.CollectNum(blogId);
    }

    @PostMapping("/iscollect")
    public boolean iscollect(@RequestBody JSONObject param){
        Integer blogId = param.getInteger("blogId");
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(tokenName);
        int i = collectMapper.blogByUser(blogId, userId);
        return (i!=0);
    }
}
